package com.jingbit.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.jingbit.utils.DateUtils;
import com.jingbit.widget.CalendarList;

public class DateRangeDialog extends CustomBaseDialog {

    private Context context;
    private String startDate, endDate;
    private IRangeSelectListener rangeSelectListener;

    public DateRangeDialog(Context context) {
        super(context, R.style.CustomDialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar_range);
        CalendarList calendarList = findViewById(R.id.calendarList);

        TextView tv_date = findViewById(R.id.tv_date);
        tv_date.setText(DateUtils.getYearMonth());
        calendarList.setOnDateSelected(new CalendarList.OnDateSelected() {
            @Override
            public void selected(String startDate1, String endDate1) {
                startDate = startDate1;
                endDate = endDate1;
            }
        });

        //设置确定按钮被点击后，向外界提供监听
        findViewById(R.id.btn_okay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(startDate) && TextUtils.isEmpty(endDate)) {
                    Toast.makeText(context, "请选择日期范围！", Toast.LENGTH_LONG).show();
                    return;
                }
                endDate = endDate == null ? startDate : endDate;
                if (rangeSelectListener != null) {
                    rangeSelectListener.onRangeSelect(startDate, endDate);
                }
                dismiss();
            }
        });

        //设置取消按钮被点击后，向外界提供监听
        findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void show(IRangeSelectListener rangeSelectListener) {
        this.rangeSelectListener = rangeSelectListener;
        showDialog(Gravity.BOTTOM, R.style.AnimBottom, 1, 0.6f);
    }

    public interface IRangeSelectListener {
        public void onRangeSelect(String startDate, String endDate);
    }
}
